# -*- coding = utf-8 -*-
# @Time    : 2025/4/8 下午7:51
# @Author  : yqk
# @File    : 哔哩哔哩登录.py
# @Software: PyCharm
import json
import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from chaojiying_Python.chaojiying import Chaojiying_Client
service = Service(executable_path='D:\python-code\chromedriver-win64\chromedriver.exe')
opt = Options()
opt.debugger_address = "127.0.0.1:8888"
def login(username,password,soft_id):
    url = 'https://www.bilibili.com/'
    browser = webdriver.Chrome(service=service, options=opt)
    browser.get(url)

    #点击登录
    login_element = (By.XPATH, '//*[@id="i_cecream"]/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div[1]/div/span')
    WebDriverWait(browser, 10).until(EC.visibility_of_element_located(login_element))
    browser.find_element(By.XPATH, '//*[@id="i_cecream"]/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div[1]/div/span').click()
    #确认弹框加载
    pw_element = (By.XPATH, '/html/body/div[4]/div/div[4]/div[2]/form/div[3]/input')
    WebDriverWait(browser, 4).until(EC.visibility_of_element_located(pw_element))
    #输入账号
    browser.find_element(By.XPATH, '//input[@placeholder="请输入账号"]').send_keys('111111111111111111')
    time.sleep(2)
    #输入密码
    browser.find_element(By.XPATH, '//input[@placeholder="请输入密码"]').send_keys('111111111111111111')
    time.sleep(1)
    #点击登录
    browser.find_element(By.XPATH, '/html/body/div[4]/div/div[4]/div[2]/div[2]/div[2]').click()
    time.sleep(1)
    pic_element = browser.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[6]/div/div/div[2]/div[1]/div/div[2]/img')
    img = pic_element.screenshot_as_png
    time.sleep(1)
    chaojiying = Chaojiying_Client(username, password, soft_id)
    locs = chaojiying.PostPic(img, 9004)['pic_str']
    print(locs)
    locs = locs.split('|')
    width = float(pic_element.size['width'])/2
    height =float(pic_element.size['height'])/2
    for loc in locs:
        x_ref = float(loc.split(',')[0])
        y_ref = float(loc.split(',')[1])
        ActionChains(browser).move_to_element_with_offset(pic_element,int(x_ref-width), int(y_ref-height)).click().perform()
        time.sleep(1)
    browser.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[6]/div/div/div[3]/a/div').click()

if __name__ == '__main__':
    with open(r'D:\python-code\chaojiying_Python\password.json', 'r', encoding='utf-8') as f:
        info = json.loads(f.read())
    password = info['password']
    username = info['username']
    soft_id = info['soft_id']
    login(username, password, soft_id)